﻿using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using PowerMonitor.Common;
using PowerMonitor.Properties;
using PowerMonitor.Upgrade;
using PowerMonitor.History;

namespace PowerMonitor
{
    public partial class PowerMonitor : Form
    {
        [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
        public static extern int SetProcessWorkingSetSize(int hProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);// 设置内存大小，用于压缩

        public ModbusDevPara InnerModPar = null;   // Modbus内部规约参数配置
        public ShowComunication FrmShowMsg = new ShowComunication();  // 显示收发报文
        public ShowDevValue FrmShowValue = new ShowDevValue();  // 显示收发报文

        INIFile inifile = null;
        private ComAssistant ComAssist = null; // 调试助手
        public PowerMonitor()
        {
            InitializeComponent();
            Application.Idle+=new EventHandler(Application_Idle);
        }
        /// <summary>
        /// 程序空闲任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Application_Idle(object sender, EventArgs e)
        {
            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                RegForm.CheckRegister(); // 检查注册信息
            });
        }
        private void PowerMonitor_Load(object sender, EventArgs e)
        {
            this.Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2;
            this.Top = (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2;
            WaitingForm waiting = new WaitingForm((obj, arg) =>
            {
                if (AppMain.LoadChlDevPara() == false)
                {
                    MessageBox.Show(Resources.txtFailedToInit);
                }
            }, false, false);
            waiting.ShowDialog();
            SetProcessWorkingSetSize((int)System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);  // 设置进程最小内存空间
            //InnerModPar.Show(mainDock);
            FrmShowValue.Show(mainDock);
            FrmShowValue.timer1.Tick+=new EventHandler(timer0_Tick);
            FrmShowMsg.Show(mainDock);
            if (LangChange.GetLangId() == 0)
            {
                tsmiLangChinese.Checked = true;
                tsmiLangEnglish.Checked = false;
            }
            else
            {
                tsmiLangChinese.Checked = false;
                tsmiLangEnglish.Checked = true;
            }
        }
      
        
        /// <summary>
        /// 内部规约装置管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiInnerDevManage_Click(object sender, EventArgs e)
        {
            AppMain.StopAllChl();
            FrmShowValue.timer1.Enabled = false;   // 停止数据刷新
            InnerModPar = new ModbusDevPara();
            InnerModPar.ShowDialog();
            FrmShowValue.RefreshChlTree();
            FrmShowMsg.RefreshChlTree();
            FrmShowValue.timer1.Enabled = true;   // 开始数据刷新
        }
        /// <summary>
        /// 启动收发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiStartRW_Click(object sender, EventArgs e)
        {
            //if (AppMain.isRegisted)
            //{
                AppMain.StartAllChl();
            //}
            //else
            //{
            //    MessageBox.Show(Resources.txtPleaseRegiste,Resources.msgTitleTip,MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
            //}
        }
        /// <summary>
        /// 停止收发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiStopRW_Click(object sender, EventArgs e)
        {
            AppMain.StopAllChl();
        }
        /// <summary>
        /// 数值监视
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiRealTimeData_Click(object sender, EventArgs e)
        {
            FrmShowValue.Show(mainDock);
            //FrmShowMsg.VisibleState = WeifenLuo.WinFormsUI.Docking.DockState.Document;
        }
        private void tsmiHistoryQuery_Click(object sender, EventArgs e)
        {
            ReportView hisForm = new ReportView();
            hisForm.ShowDialog();
          
        }
        private void tsmiStatisticChart_Click(object sender, EventArgs e)
        {
            StatisticChartView hisChart = new StatisticChartView();
            hisChart.ShowDialog();
        }
        /// <summary>
        /// 报文监视
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiMsgMonitor_Click(object sender, EventArgs e)
        {
            FrmShowMsg.Show(mainDock);
            FrmShowMsg.VisibleState = WeifenLuo.WinFormsUI.Docking.DockState.DockBottom;
        }
        private void timer0_Tick(object sender, EventArgs e)
        {
            bool isRuning = false;
            foreach (var channel in AppMain.ChlList)
            {
                isRuning = channel.GetChannelState();
                if (isRuning)
                {
                    break;
                }
            }
            tsmiInnerDevManage.Enabled = !isRuning;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            SetProcessWorkingSetSize((int)System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);  // 设置进程最小内存空间
        }

        private void tsmiComAssit_Click(object sender, EventArgs e)
        {
            if (ComAssist == null)
            {
                ComAssist = new ComAssistant();
                ComAssist.Show(mainDock);
            }
            else
            {
                if (ComAssist.IsDisposed)
                {
                    ComAssist = new ComAssistant();
                    ComAssist.Show(mainDock);
                }
                else
                {
                    ComAssist.Show(mainDock);
                }
            }
        }

        private void PowerMonitor_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void tsmiRegister_Click(object sender, EventArgs e)
        {
            RegForm frm = new RegForm();
            frm.ShowDialog();
        }
        /// <summary>
        /// 语言设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiLangEnglish_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = sender as ToolStripMenuItem;
            if (menu == null)
            {
                return;
            }
            this.WindowState = FormWindowState.Normal;
            if(menu.Name == "tsmiLangEnglish")
            {
                tsmiLangEnglish.Checked = true;
                tsmiLangChinese.Checked = false;
               LangChange.SelectLang(1);
            }
            else
            {
                tsmiLangChinese.Checked = true;
                tsmiLangEnglish.Checked = false;
                LangChange.SelectLang(0);
            }

            LangChange.SetFormLang(this, typeof(PowerMonitor));
            LangChange.SetFormLang(InnerModPar, typeof(ModbusDevPara));
            LangChange.SetFormLang(FrmShowMsg, typeof(ShowComunication));
            LangChange.SetFormLang(FrmShowValue, typeof(ShowDevValue));
            if(FrmShowValue!=null)
            FrmShowValue.RefreshChlTree();
            this.WindowState = FormWindowState.Maximized;
        }

        private void tsmiAboutCompany_Click(object sender, EventArgs e)
        {
            CompanyProfile profile = new CompanyProfile();
            profile.ShowDialog();
        }
    }
}
